<?php

class ArticleModule extends SiteMode {
    protected $table;
    protected static $selfObjectArray;
    protected function __construct($table) {
        parent::__construct();
        $this->table=!empty($table)&&validateVar($table)?$table:'likyh_article';
    }

    /** @return ArticleModule */
    public static function init($table = 'likyh_article'){
        if(!isset(self::$selfObjectArray[$table])){
            self::$selfObjectArray[$table]=new ArticleModule($table);
        }
        return self::$selfObjectArray[$table];
    }
    function add($title,$content,$type='',$remark=''){
        $data['title']=$title;
        $data['content']=$content;
        $data['type']=$type;
        $data['remark']=$remark;
        $data['site_id']=$this->siteId;
        return $this->db->insert($this->table,$data)==1 ? (int)$this->db->insertId(): -1;
    }
    function modify($id,$title = null,$content = null){
        $data['title']=$title;
        $data['content']=$content;
        return $this->db->update($this->table,$id,$data) == 1? true:false;
    }
    function delete($id){
        return $this->db->delete($this->table,$id) ==1;
    }
    function getDetail($ids){
        $sql="SELECT * FROM `{$this->table}` WHERE ".$this->db->getIdCondition($ids,'id');
        $r=$this->db->getAll($sql);
        return is_array($ids)? $r: reset($r);
    }
    function getDetailByRemark($remark){
        $sql="SELECT * FROM `{$this->table}` WHERE site_id=? and `remark`=?";
        return $this->db->getOne($sql, $this->siteId, $remark);
    }
    function getList($type=null,$rows=20,$offset=0){
        $rows=(int)$rows;
        $offset=(int)$offset;
        if(!empty($type)){
            $sql="SELECT * FROM `{$this->table}` where site_id=? and `type`=? order by `create_time` desc LIMIT $offset,$rows";
            return $this->db->getList($sql, $this->siteId,$type);

        }else{
            $sql="SELECT * FROM `{$this->table}` where site_id=? order by `create_time` desc LIMIT $offset,$rows";
            return $this->db->getList($sql, $this->siteId);
        }
    }
} 